<template>
	<view class="segmented-control" :class="styleType" :style="wrapStyle">
		<view v-for="(item, index) in values" class="segmented-control-item" :class="styleType" :key="index" :style="index === currentIndex ? activeStyle : itemStyle" @click="onClick(index)">
			<view class="bgbox">
				<view class="text" :style="index === currentIndex ? activeText : itemStyle">{{item}}</view>
				<view class="cell" :style="index === currentIndex ? activecellStyle : itemStyle"></view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'uni-segmented-control',
		props: {
			current: {
				type: Number,
				default: 0
			},
			values: {
				type: Array,
				default () {
					return [];
				}
			},
			activeColor: {
				type: String,
				default: '#007aff'
			},
			styleType: {
				type: String,
				default: 'button'
			}
		},
		data() {
			return {
				currentIndex: this.current
			}
		},
		watch: {
			current(val) {
				if (val !== this.currentIndex) {
					this.currentIndex = val;
				}
			}
		},
		computed: {
			wrapStyle() {
				let styleString = '';
				switch (this.styleType) {
					case 'text':
						styleString = `border:0;`;
						break;
					case 'jiaofei':
						styleString = `border-radius:0;border:0;color:#fff;background-color:${this.activeColor}`;
						break;
					case 'reserve':
						styleString = `border-radius:0;border:0;color:#000000;background-color:${this.activeColor}`;
						break;
					default:
						styleString = `border-color: ${this.activeColor}`;
						break;
				}
				return styleString;
			},
			itemStyle() {
				let styleString = '';
				switch (this.styleType) {
					case 'text':
						styleString = `color:#000;border-left:0;`;
						break;
					case 'jiaofei':
						styleString = `border-radius:0;border:0;color:#fff;`;
						break;
					case 'reserve':
						styleString = `border-radius:0;border:0;color:#000000;`;
						break;
					default:
						styleString = `color:${this.activeColor};border-color:${this.activeColor};`;
						break;
				}
				return styleString;
			},
			activeStyle() {
				let styleString = '';
				switch (this.styleType) {
					case 'text':
						styleString = `color:${this.activeColor};border-left:0;border-bottom-style:solid;`;
						break;
					case 'jiaofei':
						styleString = `border-radius:0;`;
						break;
					case 'reserve':
						styleString = `border-radius:0;color:#000000`;
						break;
					default:
						styleString = `color:#fff;border-color:${this.activeColor};background-color:${this.activeColor}`;
						break;
				}
				return styleString;
			},
			activeText() {
				let styleString = '';
				switch (this.styleType) {
					case 'text':
						styleString = `color:${this.activeColor};flex-shrink: 0;`;
						break;
					case 'jiaofei':
						styleString = `border-radius:0;color:#fff;font-weight: 600;`;
						break;
					case 'reserve':
						styleString = `border-radius:0;color:red;font-weight: 600;`;
						break;
					default:
						styleString = `color:#fff;border-color:${this.activeColor};background-color:${this.activeColor}`;
						break;
				}
				return styleString;
			},
			activecellStyle() {
				let styleString = '';
				switch (this.styleType) {
					case 'text':
						styleString = ``;
						break;
					case 'jiaofei':
						styleString = `color:#fff;font-weight: 600;background-color: #ffffff;`;
						break;
					case 'reserve':
						styleString = `color:#000000;font-weight: 600;background-color: red;`;
						break;
					default:
						styleString = `color:#fff;border-color:${this.activeColor};background-color:${this.activeColor}`;
						break;
				}
				return styleString;
			},
		},
		methods: {
			onClick(index) {
				if (this.currentIndex !== index) {
					this.currentIndex = index;
					this.$emit('clickItem', index);
				}
			}
		},
	}
</script>

<style>
	.segmented-control {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		width: 100%;
		font-size: 28upx;
		border-radius: 10upx;
		box-sizing: border-box;
		margin: 0 auto;
		overflow: hidden;
	}

	.segmented-control.button {
		border: 2upx solid;
	}

	.segmented-control.text {
		border: 0;
		border-radius: 0upx;
	}


	.segmented-control-item {
		flex: 1;
		text-align: center;
		line-height: 50upx;
		height: 80rpx;
		box-sizing: border-box;
		/* display: flex;
		justify-content: center; */
		margin: 0 24rpx;
	}

	.segmented-control-item.button {
		border-left: 1upx solid;
	}

	.segmented-control-item.text {
		border-left: 0;
	}

	.segmented-control-item:first-child {
		border-left-width: 0;
	}
	.bgbox {
		height: 80upx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
	.cell {
		height: 6rpx;
		width: 20%;
		border-radius: 10rpx;
	}
</style>
